//
//  ViewController1.m
//  CHENGER
//
//  Created by mac on 16/11/24.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController1.h"
#import "DatabaseTool.h"

@interface ViewController1 ()<UITableViewDataSource,UITableViewDelegate>
@property (retain, nonatomic) IBOutlet UITextField *namefield;
@property (retain, nonatomic) IBOutlet UITextField *phonefield;

@property (retain,nonatomic) UITableView *phonetable;

@property (nonatomic,copy)  NSMutableArray *phonearray;


@end

@implementation ViewController1

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _phonearray=[[NSMutableArray alloc]init];

    _phonetable=[[UITableView alloc]initWithFrame:CGRectMake(0, 260, 375, 667-160)];

    if (!_person) {

    }else{
        _namefield.text=_person.name;
        _namefield.enabled=NO;

        [_phonearray setArray:[DatabaseTool getallphonewithpeopleid:_person.peopleID]];



    }
    _phonetable.dataSource=self;
    _phonetable.delegate=self;
    
    [_phonetable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_phonetable];

}

- (IBAction)addmethod:(id)sender {
    if (_namefield.text.length==0) {
        return;
    }
    if (_phonefield.text.length==0) {
        return;
    }
    [_phonearray addObject:_phonefield.text];
    [_phonetable reloadData];

    if (_person) {

    }else{
        NSInteger peopleID=[DatabaseTool insertcontactWithName:_phonefield.text];
        _person=[[People alloc]init];
        _person.name=_namefield.text;
        _person.peopleID=peopleID;

    }
    [DatabaseTool insertPhoneWithPeopleID:_person.peopleID phone:_phonefield.text];




}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

 return    _phonearray.count;


}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text=_phonearray[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)dealloc {
    [_namefield release];
    [_phonefield release];
    [_phonetable release];
    [_person release];
    [_phonetable release];
    [super dealloc];
}
@end
